/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.aigestudio.wheelpicker.demo.slice;

import com.aigestudio.wheelpicker.WheelPicker;
import com.aigestudio.wheelpicker.demo.ResourceTable;
import com.aigestudio.wheelpicker.util.LogUtil;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.window.dialog.ToastDialog;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

/**
 * MainAbilitySlice extends AbilitySlice
 */
public class MainAbilitySlice extends AbilitySlice
    implements WheelPicker.OnItemSelectedListener, Component.ClickedListener {
    private static final String TAG = MainAbilitySlice.class.getSimpleName() + " test_wheel";

    private WheelPicker wheelLeft;

    private WheelPicker wheelCenter;

    private WheelPicker wheelRight;

    private Button gotoBtn;

    private int gotoBtnItemIndex;

    private String text = "";

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ac_preview);
    }

    @Override
    public void onActive() {
        super.onActive();
        Component component = findComponentById(ResourceTable.Id_main_wheel_left);
        if (component instanceof WheelPicker) {
            wheelLeft = (WheelPicker) component;
            wheelLeft.setOnItemSelectedListener(this);
        }
        component = findComponentById(ResourceTable.Id_main_wheel_center);
        if (component instanceof WheelPicker) {
            wheelCenter = (WheelPicker) component;
            wheelCenter.setOnItemSelectedListener(this);
        }
        component = findComponentById(ResourceTable.Id_main_wheel_right);
        if (component instanceof WheelPicker) {
            wheelRight = (WheelPicker) component;
            wheelRight.setOnItemSelectedListener(this);
        }

        component = findComponentById(ResourceTable.Id_goto_btn);
        if (component instanceof Button) {
            gotoBtn = (Button) component;
            gotoBtn.setClickedListener(this);
        }
        randomlySetGotoBtnIndex();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        wheelCenter.setSelectedItemPosition(gotoBtnItemIndex);
        randomlySetGotoBtnIndex();
    }

    private void randomlySetGotoBtnIndex() {
        gotoBtnItemIndex = (int) (Math.random() * wheelCenter.getData().size());
        gotoBtn.setText("Goto '" + wheelCenter.getData().get(gotoBtnItemIndex) + "'");
    }

    @Override
    public void onItemSelected(WheelPicker picker, Object data, int position) {
        switch (picker.getId()) {
            case ResourceTable.Id_main_wheel_left:
                text = "Left:";
                break;
            case ResourceTable.Id_main_wheel_center:
                text = "Center:";
                break;
            case ResourceTable.Id_main_wheel_right:
                text = "Right:";
                break;
            default:
                LogUtil.error(TAG, "Unhndled component for onItemSelected");
        }
        new EventHandler(EventRunner.getMainEventRunner()).postTask(
            () -> new ToastDialog(this).setText(text + data).show());
    }
}
